<?php

/**
 * Bookings Model for Room Booking Component
 * 
 * @version 1.0
 * @package    Joomla
 * @subpackage RoomBooking
 * @copyright (C) 2011 Giovanni Marzulli
 * @license		GNU/GPL
 */
// No direct access

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class RoomBookingModelBookings extends JModel {

    var $_data;

    function _buildQuery() {
        $user = & JFactory::getUser();
        $user_id = $user->get('id');
        $str = '';
        if ($user_id == 62)
            $str = '';
        else
            $str = 'AND r.id_user = ' . $user_id;
        $query = ' SELECT r.id, r.short_description, r.description, date_format( r.start, "%Y-%m-%d %H:%i" ) as start, date_format( r.end, "%Y-%m-%d %H:%i" ) as end, ' .
                ' r.id_user, room.name AS room_name, r.series, room.id AS room_id,r.total,t.cost,t.cancel_cost,t.fee FROM #__roombooking_bookings AS r LEFT OUTER JOIN ' .
                ' (#__roombooking_rooms AS room,#__roombooking_rooms_types AS t) ON (r.id_room = room.id AND room.room_type_id = t.id) WHERE start >= CURDATE() ' . $str . ' ORDER BY start';
        return $query;
    }

    function getData() {
        // Lets load the data if it doesn't already exist
        if (empty($this->_data)) {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList($query);
        }

        return $this->_data;
    }

    function getOrganizationList() {
        $query = 'SELECT * FROM #__roombooking_organizations ORDER BY id';
        return $this->getObjectList($query);
    }

    function getBuildingList() {
        $organization_id = JRequest::getVar('id_organization');
        if ($organization_id == null)
            $organization_id = 0;
        $id_room = JRequest::getVar('id_room');
        if ($id_room == null)
            $id_room = 0;
        if ($organization_id != 0)
            $query = 'SELECT b.id,b.name FROM #__roombooking_buildings AS b LEFT JOIN (#__roombooking_building_organization AS bo,#__roombooking_organizations AS o) ON (b.id = bo.id_building AND bo.id_organization = o.id) WHERE o.id = ' . $organization_id . ' ORDER BY b.id';
        else
            $query = 'SELECT rb.id,rb.name FROM #__roombooking_buildings AS rb LEFT JOIN (#__roombooking_building_organization AS rbo,#__roombooking_organizations AS ro) ON (rb.id = rbo.id_building AND rbo.id_organization = ro.id) WHERE ro.id = ' . $this->getIdOrganization() . ' ORDER BY rb.id';

//        echo $query;

        return $this->getObjectList($query);
    }

    function getIdOrganization() {
        $id_room = JRequest::getVar('id_room');
        if ($id_room == null)
            $id_room = 0;
        if ($id_room != 0) {
            $query = 'SELECT o.id FROM #__roombooking_buildings AS b LEFT JOIN (#__roombooking_building_organization AS bo,#__roombooking_organizations AS o,#__roombooking_rooms AS r) ON (b.id = bo.id_building AND bo.id_organization = o.id AND r.id_building = b.id) WHERE r.id = ' . $id_room . ' ORDER BY b.id';
//            echo $query;
            $items = $this->_getList($query);
            foreach ($items as $row) {
                return $row->id;
            }
        }else
            return 0;
    }

    function getRoomList() {
        $id_organization = JRequest::getVar('id_organization');
        if ($id_organization == null)
            $id_organization = 0;
        $id_building = JRequest::getVar('id_building');
        if ($id_building == null)
            $id_building = 0;
        $id_room = JRequest::getVar('id_room');
        if ($id_room == null)
            $id_room = 0;
//        echo $id_building;
        if ($id_building != 0)
            $query = 'SELECT * FROM #__roombooking_rooms WHERE id_building = ' . $id_building . ' ORDER BY id';
        else if ($id_organization != 0 || $id_room != 0) {
            $query = 'SELECT * FROM #__roombooking_rooms WHERE id_building = ' . $this->getIdBuilding() . ' ORDER BY id';
//            echo $query;
        }

        return $this->getObjectList($query);
    }

    function getIdBuilding() {
        $id_organization = JRequest::getVar('id_organization');
        if ($id_organization == null)
            $id_organization = 0;
        $id_room = JRequest::getVar('id_room');
        if ($id_room == null)
            $id_room = 0;
        if ($id_organization != 0) {
            $query = 'SELECT b.id FROM #__roombooking_buildings AS b LEFT JOIN (#__roombooking_building_organization AS bo,#__roombooking_organizations AS o) ON (b.id = bo.id_building AND bo.id_organization = o.id) WHERE o.id = ' . $id_organization . ' ORDER BY b.id';
        } else if ($id_room != 0) {
            $query = 'SELECT b.id FROM #__roombooking_buildings AS b LEFT JOIN (#__roombooking_building_organization AS bo,#__roombooking_organizations AS o,#__roombooking_rooms AS r) ON (b.id = bo.id_building AND bo.id_organization = o.id AND r.id_building = b.id) WHERE r.id = ' . $id_room . ' ORDER BY b.id';
        }
//        echo $query;
        $items = $this->_getList($query);
        foreach ($items as $row) {
//            echo $row->id;
            return $row->id;
        }
        return 0;
    }

    function getObjectList($query) {
        $db = & JFactory::getDBO();
        $db->setQuery($query);
        return $db->loadObjectList();
    }

    /**
     * Gets bookings
     * @return array Array of objects containing the data from the database
     */
    function getBookings($id_room) {
        $db = & JFactory::getDBO();

        $query = 'SELECT * FROM #__roombooking_bookings WHERE id_room=' . $id_room . ' AND end >= DATE_ADD(CURDATE(), INTERVAL -1 YEAR)';
        $bookings = $this->_getList($query);

        return $bookings;
    }

    /**
     * Retrieves future bookings of room different of booking with id=$id_booking
     * @return array Array of objects containing the data from the database
     */
    function getRoomBookings($id_room, $id_booking) {
        $query = 'SELECT start, end FROM #__roombooking_bookings WHERE end >= CURDATE() AND id_room = ' . $id_room . ' AND id <> ' . $id_booking;
//                $this->roomLog($query);

        $bookings = $this->_getList($query);

        return $bookings;
    }

    function roomLog($comment) {
        jimport('joomla.error.log');
        // Define the file name as a PHP constant
        define('ROOM_LOG', 'room.log.' . date('Y_m_d') . '.php');

        $log = &JLog::getInstance(ROOM_LOG);

        // Just adding a comment
        $log->addEntry(array('comment' => $comment));
    }

}
